
package org.soekrat.server.servlets.chat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.soekrat.engine.SoekratEngine;
import org.soekrat.engine.io.SoekratXml;
import org.soekrat.environment.Orb;
import org.soekrat.environment.conversationbase.SoekratData;
import org.soekrat.environment.conversationbase.SoekratDatabase;
import org.soekrat.server.io.db.DbFile;
import org.soekrat.server.io.db.Folder;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;

/**
 * <p>
 *   {@link SoekratDatabase} for server side. 
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class ServerSoekratDatabase implements SoekratDatabase 
{
  private final long m_folderKey;
   
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param folderKey Key to 
   *                  {@link Folder} with Soekrat XML.
   */
  public ServerSoekratDatabase(long folderKey)
  {
    if (folderKey<0)
    {
      throw new IllegalArgumentException("folderKey<0 : "+folderKey);
    }
    
    m_folderKey=folderKey;
  }
  
  // Specified in SoekratDatabase.
  public Map<String, SoekratData> load() throws IOException 
  {
    Map<String, SoekratData> retVal=new HashMap<String, SoekratData>();
    
    SoekratDAO dao=new SoekratDAOFactory().create();
    
    Folder folder=dao.getFolder(m_folderKey);
    
    if (folder==null)
    {
      throw new IOException("Found no folder with key : "+m_folderKey);
    }
    
    System.out.println("folderKey for the rat base: "+m_folderKey);
    
    List<DbFile> files=dao.getFolderContent(folder.id());
    for (DbFile file : files)
    {
      String name=file.name();
      
      System.out.println("Parses XML for "+name);
      
      InputStream is=dao.getInputStream(file);
      if (is!=null)
      {
        SoekratData sd=SoekratXml.parse(is);
        retVal.put(name, sd);
      }
    }
    
    return retVal;
  }

  public void save(Orb orb, SoekratEngine engine) throws IOException 
  {
    // TODO Auto-generated method stub
    
  }
    
  public void save(SoekratData... sd) throws IOException 
  {
    SoekratDAO dao=new SoekratDAOFactory().create();
    
    for (SoekratData curr : sd)
    {
      String name=curr.getName();
      
      List<DbFile> files=dao.getFile(m_folderKey, name); 
      if (files.size()==1)
      {
        DbFile file=files.get(0);
        
        byte[] bytes=getBytes(curr);
      
        @SuppressWarnings("unused")
        DbFile sfile=dao.createFile(name, m_folderKey, bytes, true, "text/xml",
          file.id(), name);
      }
    }
  }
  
  private static byte[] getBytes(SoekratData sd) throws IOException
  {
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    SoekratXml.compile(sd, baos);
    return baos.toByteArray();
  }
  
}
